package com.webtest.core;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class WebTestListener1 extends TestListenerAdapter{
   public int success;
    public int fail;
    @Override
    public void onFinish(ITestContext testContext) {
      List<ITestResult> fail_list = this.getFailedTests();
      List<ITestResult> pass_list = this.getPassedTests();
      
      
      success=pass_list.size();
      fail=fail_list.size();

      File file=new File("templates\\test2.html");
      Configuration cfg = new Configuration(Configuration.getVersion());



       try {
        cfg.setDirectoryForTemplateLoading(new File("./templates"));
        cfg.setDefaultEncoding("utf-8");
         Template template =cfg.getTemplate("dh.ftl");//demohtml
         Map map=new HashMap();
         int i=1;
         
         for (ITestResult tr : pass_list) {
            String method_name = tr.getInstanceName() + "_" + tr.getName();
            map.put("p"+i,method_name );
            map.put("yn"+i, "PASS");
            i++;
          }
         
         for (ITestResult tr : fail_list) {
            String method_name = tr.getInstanceName() + "_" + tr.getName();
            map.put("p"+i,method_name );
            map.put("yn"+i, "FAIL");
            i++;
          }
        

         
         map.put("sum",success+fail);
         map.put("fail",fail);
         
         FileWriter out=new FileWriter("./templates/test2.html");
         try {
          template.process(map, out);
        } catch (TemplateException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
         if (null != out) {
           out.flush();
         }
         out.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

      

}